﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Configuration;
using PictureProcessing.Comm;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace PictureProcessing
{
    public partial class FromGetActivateCode : Form
    {
        private static Regex RegEmail = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        public FromGetActivateCode()
        {
            InitializeComponent();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            try
            {
                string email = txtEmail.Text;

                if (string.IsNullOrEmpty(email))
                {
                    MessageBox.Show("请输入邮箱");
                    return;
                }
                else
                {

                    if (!IsEmail(email))
                    {
                        MessageBox.Show("输入的电子邮件格式不正确！如(yongjie@163.com)。");
                        return;
                    }
                    else
                    {
                        string mac = CheckActivateCode.GetMacAddress();
                        string SreverUrl = ConfigurationManager.AppSettings["ServerUrl"];
                        string url = SreverUrl + "?mac=" + mac + "&email=" + email;
                        string result = CheckActivateCode.Get(url);
                        var ss = JsonConvert.DeserializeObject<OpenAPIResponse>(result);
                        if (ss.code == "200")
                        {
                            MessageBox.Show("邮件发送成功!");
                            return;
                        }
                    }

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("发送邮件失败:"+ex.Message);
            }





        }

        /// <summary>
        /// 是否是Email
        /// </summary>
        public static bool IsEmail(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                Match m = RegEmail.Match(value);
                return m.Success;
            }
            else
            {
                return false;
            }
        }
    }
}
